package com.example.demo.config;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.example.demo.interceptor.BaseInterceptor;

/**
 * 工程配置**/
@Configuration
public class WebConfig implements WebMvcConfigurer{
	
	private static final Logger logger = LogManager.getLogger(WebConfig.class);
	
	@Autowired
	private BaseInterceptor baseInterceptor;
	
	@Override
    public void addInterceptors(InterceptorRegistry registry) {
		
		logger.info("WebConfig addInterceptors");
        registry.addInterceptor(baseInterceptor).addPathPatterns("/**");
    }

	@Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOrigins("*").allowCredentials(true).allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS", "HEAD").maxAge(3600 * 24);
    }
	
	// 载入自定义yml
	/*
	 * @Bean public static PropertySourcesPlaceholderConfigurer properties() {
	 * PropertySourcesPlaceholderConfigurer configurer = new
	 * PropertySourcesPlaceholderConfigurer(); YamlPropertiesFactoryBean yaml = new
	 * YamlPropertiesFactoryBean(); yaml.setResources(new
	 * ClassPathResource("config/settings.yml"));// class引入
	 * configurer.setProperties(yaml.getObject()); return configurer; }
	 */
}
